package com.cade.agriculturemall.view.personalCenter.historyTrack;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseActivity;
import com.cade.agriculturemall.model.GoodsDetailEntity;
import com.cade.agriculturemall.model.ItemFavouriteEntity;
import com.cade.agriculturemall.view.classify.goodsdetail.GoodsDetailActivity;
import com.cade.agriculturemall.view.personalCenter.collect.CollectAdapter;
import com.cade.agriculturemall.widget.EmptyLayout;

import java.util.List;

public class HistoryTrackActivity extends BaseActivity<HistoryTrackContract.IPresenter> implements HistoryTrackContract.IView {
    private List<ItemFavouriteEntity> entity;
    private EmptyLayout emptyLayout;
    private SwipeMenuListView mListView;
    private HistoryTrackAdapter mAdapter = new HistoryTrackAdapter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        setupToolbar(true);

    }

    @Override
    public void getHistoryList(List<ItemFavouriteEntity> list) {
        if (list.size()!=0) {
            emptyLayout.reset();
            entity = list;
            mAdapter.setList(entity);
            mAdapter.notifyDataSetChanged();
        }else {
            emptyLayout.empty();
        }
    }

    @Override
    public void setupView() {
        emptyLayout = (EmptyLayout) findViewById(R.id.track_emptyLayout);
        mListView = (SwipeMenuListView) findViewById(R.id.track_listView);

        if (mAdapter != null) {
            mListView.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);
            mListView.setAdapter(mAdapter);
        }
        mListView.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);

        // set creator
        mListView.setMenuCreator(creator);
        // step 2. listener item click event
        mListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                ItemFavouriteEntity item = entity.get(position);
                switch (index) {
//                    case 0:
//                        // open
////                        open(item);
//                        GoodsDetailEntity goodEntity = entity.get(position).getProId();
//                        mPresenter.transformIntoShopCar(goodEntity);
//                        mPresenter.delete(entity.get(position).getObjectId());
//                        entity.remove(entity.get(position));
//                        mAdapter.notifyDataSetChanged();
//                        break;
                    case 1:
                        // delete
//					delete(item);
                        Toast.makeText(mActivity, position + "", Toast.LENGTH_SHORT).show();
                        mPresenter.delete(entity.get(position).getObjectId());
//                        mAdapter.setList(entity);
                        entity.remove(entity.get(position));
                        mAdapter.notifyDataSetChanged();
                        break;
                }
                return false;
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getApplicationContext(), GoodsDetailActivity.class);
                intent.putExtra("objectId", entity.get(position).getProId().getObjectId());
                startActivity(intent);
            }
        });
        emptyLayout.setOnEmptyLayoutListener(new EmptyLayout.OnEmptyLayoutListener() {
            @Override
            public boolean onReloadClick(EmptyLayout layout) {
                mPresenter.downLoadData();
                return true;
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getApplicationContext(),GoodsDetailActivity.class);
                intent.putExtra("objectId", entity.get(position).getProId().getObjectId());
                startActivity(intent);
            }
        });
        emptyLayout.setOnEmptyLayoutListener(new EmptyLayout.OnEmptyLayoutListener() {
            @Override
            public boolean onReloadClick(EmptyLayout layout) {
                mPresenter.downLoadData();
                return true;
            }
        });
    }

    SwipeMenuCreator creator = new SwipeMenuCreator() {

        @Override
        public void create(SwipeMenu menu) {
//            // create "open" item
//            SwipeMenuItem openItem = new SwipeMenuItem(
//                    getApplicationContext());
//            // set item background
//            openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
//                    0xCE)));
//            // set item width
//            openItem.setWidth(dp2px(90));
//            // set item title
////            openItem.setTitle("Open");
//            openItem.setIcon(R.mipmap.shopping_cart);
//            // set item title fontsize
////            openItem.setTitleSize(18);
//            // set item title font color
//            openItem.setTitleColor(Color.WHITE);
//            // add to menu
//            menu.addMenuItem(openItem);

            // create "delete" item
            SwipeMenuItem deleteItem = new SwipeMenuItem(
                    getApplicationContext());
            // set item background
            deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                    0x3F, 0x25)));
            // set item width
            deleteItem.setWidth(dp2px(90));
            // set a icon
            deleteItem.setIcon(R.mipmap.delete);
            // add to menu
            menu.addMenuItem(deleteItem);
        }
    };

    @Override
    public void setupEvent() {

    }

    @Override
    public void setupData() {

    }

    @Override
    public HistoryTrackContract.IPresenter setupPresenter() {
        return new HistoryTrackPresenter(this);
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }

    @Override
    protected void onResume() {
        super.onResume();
        emptyLayout.loading();
        mPresenter.downLoadData();
    }
}

